package cn.powerone.scm.service.impl;

import java.util.List;

import cn.powerone.common.config.RuoYiConfig;
import cn.powerone.common.utils.DateUtils;
import cn.powerone.common.utils.file.FileUtils;
import cn.powerone.scm.domain.*;
import cn.powerone.scm.service.*;
import lombok.RequiredArgsConstructor;
import org.aspectj.apache.bcel.generic.FieldOrMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.powerone.scm.mapper.ScmAttachmentLinkageMapper;

/**
 * 全局附件Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-08-22
 */
@Service
@RequiredArgsConstructor
public class ScmAttachmentLinkageServiceImpl implements IScmAttachmentLinkageService 
{
    private final ScmAttachmentLinkageMapper scmAttachmentLinkageMapper;

    /**
     * 查询全局附件
     * 
     * @param id 全局附件主键
     * @return 全局附件
     */
    @Override
    public ScmAttachmentLinkage selectScmAttachmentLinkageById(Long id)
    {
        return scmAttachmentLinkageMapper.selectScmAttachmentLinkageById(id);
    }

    /**
     * 查询全局附件列表
     * 
     * @param scmAttachmentLinkage 全局附件
     * @return 全局附件
     */
    @Override
    public List<ScmAttachmentLinkage> selectScmAttachmentLinkageList(ScmAttachmentLinkage scmAttachmentLinkage)
    {
        return scmAttachmentLinkageMapper.selectScmAttachmentLinkageList(scmAttachmentLinkage);
    }

    /**
     * 新增全局附件
     * 
     * @param scmAttachmentLinkage 全局附件
     * @return 结果
     */
    @Override
    public int insertScmAttachmentLinkage(ScmAttachmentLinkage scmAttachmentLinkage)
    {
        // 如果status 为空， 则默认为1
        if (scmAttachmentLinkage.getAttachmentStatus() == null){
            scmAttachmentLinkage.setAttachmentStatus(1L);
        }
        // 如果attachmentName 为空，则使用attachmentUrl最后一段命名
        if (scmAttachmentLinkage.getAttachmentName() == null || scmAttachmentLinkage.getAttachmentName().isEmpty()) {
            scmAttachmentLinkage.setAttachmentName(scmAttachmentLinkage.getAttachmentUrl().substring(scmAttachmentLinkage.getAttachmentUrl().lastIndexOf("/") + 1));
        }

        scmAttachmentLinkage.setCreateTime(DateUtils.getNowDate());
        return scmAttachmentLinkageMapper.insertScmAttachmentLinkage(scmAttachmentLinkage);
    }

    /**
     * 修改全局附件
     * 
     * @param scmAttachmentLinkage 全局附件
     * @return 结果
     */
    @Override
    public int updateScmAttachmentLinkage(ScmAttachmentLinkage scmAttachmentLinkage)
    {
        scmAttachmentLinkage.setUpdateTime(DateUtils.getNowDate());
        return scmAttachmentLinkageMapper.updateScmAttachmentLinkage(scmAttachmentLinkage);
    }

    /**
     * 批量删除全局附件
     * 
     * @param ids 需要删除的全局附件主键
     * @return 结果
     */
    @Override
    public int deleteScmAttachmentLinkageByIds(Long[] ids)
    {
        // 删除前先遍历和查询所有记录，取出attachmentUrl，删除的时候，删除掉对应的文件
        for(Long id : ids){
            ScmAttachmentLinkage scmAttachmentLinkage = scmAttachmentLinkageMapper.selectScmAttachmentLinkageById(id);
            String attachmentUrl = scmAttachmentLinkage.getAttachmentUrl();
            if(attachmentUrl != null && !attachmentUrl.isEmpty()){
                String filePath = RuoYiConfig.getProfile() + attachmentUrl.substring(8);
                FileUtils.deleteFile(filePath);
            }
        }
        return scmAttachmentLinkageMapper.deleteScmAttachmentLinkageByIds(ids);
    }

    /**
     * 删除全局附件信息
     * 
     * @param id 全局附件主键
     * @return 结果
     */
    @Override
    public int deleteScmAttachmentLinkageById(Long id)
    {
        return scmAttachmentLinkageMapper.deleteScmAttachmentLinkageById(id);
    }
}
